package com.yxf.variousshapes3d;

import android.animation.ValueAnimator;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.LinearInterpolator;

import com.yxf.variousshapes3d.programs.ShapeProgram;
import com.yxf.variousshapes3d.shapes.Ball;
import com.yxf.variousshapes3d.shapes.Cube;
import com.yxf.variousshapes3d.shapes.Tetrahedron;

public class MainActivity extends AppCompatActivity {

    //1,定义GLSurfaceView对象,这个View提供了OpenGL ES的显示窗口
    private GLSurfaceView glSurfaceView;
    private int lastX, lastY;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //2,创建GLSurfaceView对象
        glSurfaceView = new GLSurfaceView(this);
        //3,设置OpenGL ES版本为2.0
        glSurfaceView.setEGLContextClientVersion(2);
        //4,设置渲染器
        MyRenderer renderer = new MyRenderer(this);
        glSurfaceView.setRenderer(renderer);

        //添加Program
        final ShapeProgram program = new ShapeProgram();
        program.addShape(new Tetrahedron(new Point(0f, 0.8f, 0f), 0.4f));
        program.addShape(new Cube(new Point(0f, 0f, 0f), 0.6f));
        program.addShape(new Ball(new Point(0f, -0.8f, 0f), 0.4f, 108));
        renderer.addProgram(program);

        //5,设置GLSurfaceView为主窗口
        setContentView(glSurfaceView);
        glSurfaceView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        lastX = (int) event.getX();
                        lastY = (int) event.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int x = (int) event.getX();
                        int y = (int) event.getY();
                        int dx = x - lastX;
                        int dy = y - lastY;
                        lastX = x;
                        lastY = y;
                        program.rotate(dy / 10f,dx / 10f, 0f);
                        break;
                }
                return true;
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        //6.1,当Activity暂停时暂停glSurfaceView
        glSurfaceView.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //6.2,当Activity恢复时恢复glSurfaceView
        glSurfaceView.onResume();
    }
}
